package org.jetnuke.util;

import org.apache.log4j.Logger;

import java.io.File;
import java.util.Calendar;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.ServletContext;

public class Util {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Util.class);

	static Hashtable resourcesTable = new Hashtable();
	ServletContext servletContext;

	public Util(ServletContext servletContext) {
		this.servletContext = servletContext;

	}

	public int getMonth(Calendar gc) {
		if (logger.isDebugEnabled()) {
			logger.debug("getMonth(Calendar) - start"); //$NON-NLS-1$
		}

		int returnint = gc.get(Calendar.MONTH);
		if (logger.isDebugEnabled()) {
			logger.debug("getMonth(Calendar) - end"); //$NON-NLS-1$
		}
		return returnint;
	}

	public int getYear(Calendar gc) {
		if (logger.isDebugEnabled()) {
			logger.debug("getYear(Calendar) - start"); //$NON-NLS-1$
		}

		int returnint = gc.get(Calendar.YEAR);
		if (logger.isDebugEnabled()) {
			logger.debug("getYear(Calendar) - end"); //$NON-NLS-1$
		}
		return returnint;
	}

	public int getDay(Calendar gc) {
		if (logger.isDebugEnabled()) {
			logger.debug("getDay(Calendar) - start"); //$NON-NLS-1$
		}

		int returnint = gc.get(Calendar.DAY_OF_MONTH);
		if (logger.isDebugEnabled()) {
			logger.debug("getDay(Calendar) - end"); //$NON-NLS-1$
		}
		return returnint;
	}

	public String getDayS(Calendar gc) {
		if (logger.isDebugEnabled()) {
			logger.debug("getDayS(Calendar) - start"); //$NON-NLS-1$
		}

		String returnString = String.valueOf(gc.get(Calendar.DAY_OF_MONTH));
		if (logger.isDebugEnabled()) {
			logger.debug("getDayS(Calendar) - end"); //$NON-NLS-1$
		}
		return returnString;
	}

	public boolean fileExists(String resourceFile) {
		if (logger.isDebugEnabled()) {
			logger.debug("fileExists(String) - start"); //$NON-NLS-1$
		}

		// System.out.println(resourceFile);
		try {
			String path = servletContext.getRealPath("WEB-INF/classes/"
					+ resourceFile);
			Boolean exists = (Boolean) resourcesTable.get(path);
			if (exists == null) {
				File f = new File(path);
				if (f.exists() && f.canRead()) {
					exists = Boolean.TRUE;
				} else {
					exists = Boolean.FALSE;
				}
				resourcesTable.put(path, exists);
			}
			boolean returnboolean = exists.booleanValue();
			if (logger.isDebugEnabled()) {
				logger.debug("fileExists(String) - end"); //$NON-NLS-1$
			}
			return returnboolean;
		} catch (Throwable e) {
			logger.warn("fileExists(String) - exception ignored", e); //$NON-NLS-1$
		}

		if (logger.isDebugEnabled()) {
			logger.debug("fileExists(String) - end"); //$NON-NLS-1$
		}
		return false;
	}

	public boolean existsOnSortedList(Object element, List sortedList) {
		if (logger.isDebugEnabled()) {
			logger.debug("existsOnSortedList(Object, List) - start"); //$NON-NLS-1$
		}

		int index = Collections.binarySearch(sortedList, element);

		boolean returnboolean = index > -1;
		if (logger.isDebugEnabled()) {
			logger.debug("existsOnSortedList(Object, List) - end"); //$NON-NLS-1$
		}
		return returnboolean;
	}
}
